//
//  AlertContentView.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/9/5.
//  Copyright © 2018年 四川中农. All rights reserved.
//


import UIKit

typealias BackStringValue = (String)->()
typealias BackIntValue = (Int)->()

class AlertContentView: UIView {
    
    var effectView:EffectView!
    
    var backClosure:TouchBlock!
    
    var backPassWordValue:BackStringValue!
    
    var type = 0 // 0 alert  1 成功弹窗
    
    var leftClosure:TouchBlock!
    
    var timer:Timer!
    
    var countDown = 3

    lazy var alertView:MyCustomAlert = {
      let view = Bundle.main.loadNibNamed("MyCustomAlert", owner: nil, options: nil)?.first as! MyCustomAlert
        view.frame = CGRect.init(x: (UIScreen.ScreenWidth - 250)/2, y: (UIScreen.ScreenHeight - 150)/2, width: 250, height: 150)
        view.center = self.center
        self.addSubview(view)
        view.alpha = 0
        view.setCornerRadio(radio: 20, isMask: false)
//        view.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 0), radius: 20, opacity: 0.01)
        view.leftClosure = { [weak self] in
            if self?.leftClosure != nil {
                self?.leftClosure()
            }
            self?.hiden()
        }
        view.rightClosure = { [weak self] in
            if self?.backClosure != nil {
                self?.backClosure()
            }
            self?.hiden()
        }
        return view
    }()
    
    lazy var successAlert:SuccessAlert = {
        let view = Bundle.main.loadNibNamed("SuccessAlert", owner: nil, options: nil)?.first as! SuccessAlert
        view.frame = CGRect.init(x: (UIScreen.ScreenWidth - 250)/2, y: (UIScreen.ScreenHeight - 150)/2, width: 250, height: 150)
        view.center = self.center
        self.addSubview(view)
        view.setCornerRadio(radio: 20, isMask: false)
//        view.setShadow(color: "000000", offset: CGSize.init(width: 0, height: 0), radius: 20, opacity: 0.01)
        view.closeClosure = { [weak self] in
            self?.hidenSuccessAlert()
            if self?.leftClosure != nil {
                self?.leftClosure()
            }
        }
        return view
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.effectView = EffectView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        self.effectView.alpha = 0
        self.addSubview(self.effectView)
//        self.effectView.hidenClosure = { [weak self]  in
//
//        }
    }
    
    func showSuccessAlert(){
        self.alpha = 1
        self.successAlert.alpha = 0
        UIView.animate(withDuration: 0.2, animations: {
            self.effectView.alpha = 1
            self.successAlert.alpha = 1
        }) { (_) in
            if self.timer != nil {
                self.timer.invalidate()
                self.timer = nil
            }
            self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.timeClick), userInfo: nil, repeats: true)
        }
    }
    
    @objc func timeClick(){
        self.countDown -= 1
        self.successAlert.button.setTitle("返回（" + "\(self.countDown)" + "）", for: .normal)
        if self.countDown == 0 {
            self.timer.invalidate()
            self.timer = nil
            self.hidenSuccessAlert()
            if self.leftClosure != nil {
                self.leftClosure()
            }
        }
    }
    
    func show(withTitle title:String){
        self.alpha = 1
        self.alertView.titleLabel.text = title
        UIView.animate(withDuration: 0.2) {
            self.effectView.alpha = 1
            self.alertView.alpha = 1
        }
    }
    
    func hidenSuccessAlert(){
        UIView.animate(withDuration: 0.2, animations: {
            self.effectView.alpha = 0
            self.successAlert.alpha = 0
            self.alpha = 0
        })
    }
    
    func hiden(){
        
        UIView.animate(withDuration: 0.2, animations: {
            self.effectView.alpha = 0
            self.alertView.alpha = 0
            self.alpha = 0
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
